package com.bitin.adorn.demo.api;

import com.bitin.adorn.demo.model.Account;
import com.bitin.adorn.demo.model.BankCard;
import com.bitin.adorn.demo.model.Role;
import com.bitin.adorn.demo.model.User;
import com.bitin.adorn.demo.repository.AccountRepository;
import com.bitin.adorn.demo.repository.BankCardRepository;
import com.bitin.adorn.demo.repository.RoleRepository;
import com.bitin.adorn.demo.repository.UserRepository;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
public class AccountController {

    private final UserRepository userRepository;
    private final RoleRepository roleRepository;
    private final AccountRepository accountRepository;
    private final BankCardRepository bankCardRepository;

    public AccountController(UserRepository userRepository, RoleRepository roleRepository, AccountRepository accountRepository, BankCardRepository bankCardRepository) {
        this.userRepository = userRepository;
        this.roleRepository = roleRepository;
        this.accountRepository = accountRepository;
        this.bankCardRepository = bankCardRepository;
    }

    @PostMapping("/save")
    public User build(){
        Set<BankCard> cards = new HashSet<>();

        for (int i = 0; i < 5; i++) {
            BankCard card = BankCard.builder()
                    .id(40000L + i)
                    .name("银行卡0" + i)
                    .build();
            cards.add(card);
        }

        Account account = Account.builder()
                .id(44000L)
                .balance(BigDecimal.valueOf(45637.24547))

                .build();

        User user = User.builder()
                .id(4440000L)
                .name("李四")
                //.account(account)
                .cards(cards)
                .build();

        return this.userRepository.save(user);
    }

    @PutMapping("account/{id}/rname")
    public Account update(@PathVariable(name = "id") Long id, @RequestParam(name = "name") String name){
        Account account = this.accountRepository.findById(id).orElseThrow();
        //account.rname(name);
        return this.accountRepository.save(account);
    }

    @GetMapping("/user")
    public List<User> user(){
        return this.userRepository.findAll();
    }

    @PostMapping("/user/{userId}/role")
    public Role addRole(
            @PathVariable(name = "userId") Long userId,
            @RequestParam(name = "name") String name
    ){
        User user = this.userRepository.findById(userId).orElseThrow();
        Role role = Role.builder()
                .id(168888L)
                .name(name)
                .user(user)
                .build();
        return this.roleRepository.save(role);
    }

    @PutMapping("/user/bank/card/approve")
    public List<User> approve(){
        List<User> collect = this.userRepository.findAll().stream().map(User::approve).collect(Collectors.toList());
        return this.userRepository.saveAll(collect);
    }

    @GetMapping("/bank/card")
    public List<BankCard> bankCard(){
        return this.bankCardRepository.findAll();
    }

//    @GetMapping("/account")
//    public List<Account> account(){
//        return this.accountRepository.findAll();
//    }
//
//    @GetMapping("/update/{id}")
//    public Account update(@PathVariable("id") Long id){
//        Account account = this.accountRepository.findById(id).orElseThrow();
//        account.rname(BigDecimal.valueOf(4678374.165451785), "王麻子");
//        return this.accountRepository.save(account);
//    }
}
